#include "ActionManager.h"

//@author U095387X
void ActionManager::initialize(User *curUser){
	_curUser = curUser;
}

//@author U095387X
Action ActionManager::getLastAction(){
	return _ActionStack.top();
}

//@author U095387X
void ActionManager::addIncomeAction(Income newIncome, e_action action){
	_IncomeList.push_back(newIncome);

	Action newAction(action, eIncome );

	_ActionStack.push(newAction);

	return;
}

//@author U095387X
void ActionManager::addExpenseAction(Expense newExpense, e_action action){
	_ExpenseList.push_back(newExpense);

	Action newAction(action, eExpense);

	_ActionStack.push(newAction);

	return;
}

//@author U095387X
void ActionManager::addWishAction(Wish newWish, e_action action){
	_WishList.push_back(newWish);

	Action newAction( action, eWish);

	_ActionStack.push(newAction);

	return;
}

//@author U095387X
void ActionManager::addLoanAction(Loan newLoan, e_action action){
	_LoanList.push_back(newLoan);

	Action newAction(action, eLoan);

	_ActionStack.push(newAction);

	return;
}

//@author U095387X
int ActionManager::getNumberOfLastActions(){
	return _ActionStack.size();
}

//@author U095387X
void ActionManager::undoAction(){

	if(getNumberOfLastActions() < 1)
		return;

	Action ActionToBeUndo = _ActionStack.top();

	list<Expense>::iterator it1=_ExpenseList.end(), it11=_ExpenseList.end();
	list<Income>::iterator it2=_IncomeList.end(), it22=_IncomeList.end();
	list<Wish>::iterator it3=_WishList.end(), it33=_WishList.end();
	list<Loan>::iterator it4=_LoanList.end(), it44=_LoanList.end();

	if(ActionToBeUndo.getLastEAction() == Update){
		
		switch(ActionToBeUndo.getLastEType()){
			case eExpense:
				--it1;
				--it11;
				--it11;
				_curUser->updateExpense(*it1, *it11);
				_ExpenseList.pop_back();
				_ExpenseList.pop_back();
				break;
			case eIncome:
				--it2;
				--it22;
				--it22;
				_curUser->updateIncome(*it2, *it22);
				_IncomeList.pop_back();
				_IncomeList.pop_back();
				break;
			case eWish:
				--it3;
				--it33;
				--it33;
				_curUser->updateWish(*it3, *it33);
				_WishList.pop_back();
				_WishList.pop_back();
				break;
			case eLoan:
				--it4;
				--it44;
				--it44;
				_curUser->updateLoan(*it4, *it44);
				_LoanList.pop_back();
				_LoanList.pop_back();
				break;
		}
		_ActionStack.pop();
		_ActionStack.pop();

	}
	else if(ActionToBeUndo.getLastEAction() == Delete){
		switch(ActionToBeUndo.getLastEType()){
			case eExpense:
				_curUser->addExpense(_ExpenseList.back());
				_ExpenseList.pop_back();
				break;
			case eIncome:
				_curUser->addIncome(_IncomeList.back());
				_IncomeList.pop_back();
				break;
			case eWish:
				_curUser->addWish(_WishList.back());
				_WishList.pop_back();
				break;
			case eLoan:
				_curUser->addLoan(_LoanList.back());
				_LoanList.pop_back();
				break;
		}
		_ActionStack.pop();
	}else{
		switch(ActionToBeUndo.getLastEType()){
			case eExpense:
				_curUser->deleteExpense(_ExpenseList.back());
				_ExpenseList.pop_back();
				break;
			case eIncome:
				_curUser->deleteIncome(_IncomeList.back());
				_IncomeList.pop_back();
				break;
			case eWish:
				_curUser->deleteWish(_WishList.back());
				_WishList.pop_back();
				break;
			case eLoan:
				_curUser->deleteLoan(_LoanList.back());
				_LoanList.pop_back();
				break;
		}
		_ActionStack.pop();
	}

	return;
}


//@author U095387X
string ActionManager::LastActionToString(){
	ostringstream output;

	Action lastAction = getLastAction();

	if(lastAction.getLastEAction() == Add)
		output << "ADD ";
	if(lastAction.getLastEAction()  == Delete)
		output << "DELETE ";
	if(lastAction.getLastEAction()  == Update)
		output << "UPDATE ";

	if(lastAction.getLastEType() == eExpense)
		output << "Expense - ";
	if(lastAction.getLastEType() == eIncome)
		output << "Income - ";
	if(lastAction.getLastEType() == eWish)
		output << "Wish - ";
	if(lastAction.getLastEType() == eLoan)
		output << "Loan - ";

	if(lastAction.getLastEType() == eExpense)
		output << "$" << _ExpenseList.back().getAmount();
	if(lastAction.getLastEType() == eIncome)
		output << "$" << _IncomeList.back().getAmount();
	if(lastAction.getLastEType() == eWish)
		output << "$" << _WishList.back().getAmount();
	if(lastAction.getLastEType() == eLoan)
		output << "$" << _LoanList.back().getAmount();

	if(lastAction.getLastEType() == eExpense)
		output << " for " << _ExpenseList.back().getName();
	if(lastAction.getLastEType() == eIncome)
		output << " for " << _IncomeList.back().getName();
	if(lastAction.getLastEType() == eWish)
		output << " for " << _WishList.back().getName();
	if(lastAction.getLastEType() == eLoan)
		output << " for " << _LoanList.back().getName();

	return output.str();
}
